package com.shunde.xunjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.shunde.xunjian.entity.ComplaintRegistration;
import com.shunde.xunjian.entity.ReportOrders;
import com.shunde.xunjian.entity.VerificationReport;
import com.shunde.xunjian.mapper.dbnetworkshunde.ComplaintRegistrationMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.ReportOrdersMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.VerificationReportMapper;
import com.shunde.xunjian.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service
public class ComplaintRegistrationServiceImpl {

    @Autowired
    private ComplaintRegistrationMapper complaintRegistrationMapper;

    @Autowired
    private ReportOrdersMapper reportOrdersMapper;

    @Autowired
    private VerificationReportMapper verificationReportMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");


    //PC端投诉列表
    public Object ComplaintRegistrationResults(String complaintRegistrationStr, Integer current, Integer pageSize) throws ParseException {
        ComplaintRegistration complaintRegistration = new Gson().fromJson(complaintRegistrationStr, ComplaintRegistration.class);
        QueryWrapper<ComplaintRegistration> complaintRegistrationQueryWrapper = new QueryWrapper<>();
        IPage<ComplaintRegistration> page = new Page<>(current, pageSize);
        if (complaintRegistrationStr != null) {
            if (complaintRegistration.getComplaintProblem() != null) {
                complaintRegistrationQueryWrapper.eq("complaint_problem", complaintRegistration.getComplaintProblem());
            }
            if (complaintRegistration.getTownStreet() != null) {
                complaintRegistrationQueryWrapper.eq("town_street", complaintRegistration.getTownStreet());
            }
            if (complaintRegistration.getReviewStatus() != null) {
                complaintRegistrationQueryWrapper.eq("review_status", complaintRegistration.getReviewStatus());
            }
            if (complaintRegistration.getSTime() != null && complaintRegistration.getETime() != null) {
                complaintRegistrationQueryWrapper.between("complaint_time", sdf.parse(sdf.format(sdf1.parse(complaintRegistration.getSTime()))), sdf.parse(sdf.format(sdf1.parse(complaintRegistration.getETime()))));
            }
        }
        IPage<ComplaintRegistration> complaintRegistrationIPage = complaintRegistrationMapper.selectPage(page, complaintRegistrationQueryWrapper.orderByAsc("complaint_time"));
        return complaintRegistrationIPage;
    }

    //PC端投诉新增
    public Integer ComplaintUpload(String complaintStr) {
        ComplaintRegistration complaintRegistration = new Gson().fromJson(complaintStr, ComplaintRegistration.class);
        Integer result = 0;
        if (!Utils.isEntityEmpty(complaintRegistration)) {
            // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            complaintRegistration.setComplaintTime(new Timestamp(System.currentTimeMillis()));
            // 将当前时间往后延三天
            calendar.add(Calendar.DATE, 3);
            complaintRegistration.setSceneReviewDeadline(new Timestamp(calendar.getTimeInMillis()));
            complaintRegistration.setReviewStatus("核查中");
            result = complaintRegistrationMapper.insert(complaintRegistration);
        }
        return result;
    }

    public Integer ComplaintDelete(String complaintRegistrationStr) {
        ComplaintRegistration complaintRegistration = new Gson().fromJson(complaintRegistrationStr, ComplaintRegistration.class);
        QueryWrapper<ComplaintRegistration> complaintRegistrationQueryWrapper = new QueryWrapper<>();
        Integer result = 0;
        if (!Utils.isEntityEmpty(complaintRegistration)) {
            if (complaintRegistration.getComplaintId() != null) {
                complaintRegistrationQueryWrapper.eq("complaint_id", complaintRegistration.getComplaintId());
                result = complaintRegistrationMapper.delete(complaintRegistrationQueryWrapper);
            }
        }
        return result;
    }

    //APP端投诉列表
    public Object queryComplaints(String complaintRegistrationStr, Integer current, Integer pageSize) {
        ComplaintRegistration complaintRegistration = new Gson().fromJson(complaintRegistrationStr, ComplaintRegistration.class);
        QueryWrapper<ComplaintRegistration> complaintRegistrationQueryWrapper = new QueryWrapper<>();
        complaintRegistrationQueryWrapper.select("complaint_id,complaint_problem,problem_description,address,complaint_time,scene_picture");
        IPage<ComplaintRegistration> page = new Page<>(current, pageSize);
        if (complaintRegistrationStr != null) {
            if (complaintRegistration.getTownStreet() != null) {
                complaintRegistrationQueryWrapper.eq("town_street", complaintRegistration.getTownStreet());
            }
            if (complaintRegistration.getReviewStatus() != null) {
                complaintRegistrationQueryWrapper.eq("review_status", complaintRegistration.getReviewStatus());
            }
        }
        IPage<ComplaintRegistration> complaintRegistrationIPage = complaintRegistrationMapper.selectPage(page, complaintRegistrationQueryWrapper);
        return complaintRegistrationIPage;
    }

    //投诉详情页
    public Object ComplaintRegistrationInfo(Integer complaintId) {
        QueryWrapper<ComplaintRegistration> complaintRegistrationQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ReportOrders> reportOrdersQueryWrapper = new QueryWrapper<>();
        QueryWrapper<VerificationReport> verificationReportQueryWrapper = new QueryWrapper<>();

        complaintRegistrationQueryWrapper.eq("complaint_id", complaintId);
        ComplaintRegistration complaintRegistration = new ComplaintRegistration();
        complaintRegistration = complaintRegistrationMapper.selectById(complaintId);
        VerificationReport verificationReport = new VerificationReport();
        verificationReport = verificationReportMapper.selectOne(verificationReportQueryWrapper.eq("complaint_id", complaintId));
        ReportOrders reportOrders = new ReportOrders();
        if (verificationReport != null) {
            reportOrders=reportOrdersMapper.selectOne(reportOrdersQueryWrapper.eq("order_id", verificationReport.getOrderId()));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("complaintRegistration", complaintRegistration);
        map.put("verificationReport", verificationReport);
        map.put("reportOrders", reportOrders);
        return map;
    }

}
